package engine

import (
	"github.com/xuxusheng/crawler/fetcher"
	"log"
	"fmt"
)

type SimpleEngine struct{}

func (e SimpleEngine) Run(seeds ...Request) {
	var requests []Request
	for _, r := range seeds {
		requests = append(requests, r)
	}

	for len(requests) > 0 {
		r := requests[0]
		requests = requests[1:]

		/*		// 请求 url
				log.Printf("Fetching %s", r.Url)

				body, err := fetcher.Fetch(r.Url)
				if err != nil {
					log.Printf("Fetcher: error fetching url %s: %v", r.Url, err)
					continue
				}
				parseResult := r.ParserFunc(body)
				requests = append(requests, parseResult.Requests...)*/

		parseResult, err := worker(r)
		if err != nil {
			fmt.Printf("worker error!")
			continue
		}
		requests = append(requests, parseResult.Requests...)

		for _, item := range parseResult.Items {
			log.Printf("Got item %v", item)
		}
	}
}

func worker(r Request) (ParseResult, error) {
	log.Printf("Fetching %s", r.Url)
	body, err := fetcher.Fetch(r.Url)
	if err != nil {
		log.Printf("Fetcher: error "+"fetching url %s: %v", r.Url, err)
		return ParseResult{}, err
	}
	return r.ParserFunc(body), nil
}
